package com.lanyan.system.controller.system;

import java.math.BigDecimal;
import java.util.List;
import java.util.Date;
import javax.servlet.http.HttpServletResponse;

import com.lanyan.common.utils.StringUtils;
import com.lanyan.framework.web.service.TokenService;
import com.lanyan.system.domain.Account;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.lanyan.common.annotation.Log;
import com.lanyan.common.core.controller.BaseController;
import com.lanyan.common.core.domain.AjaxResult;
import com.lanyan.common.enums.BusinessType;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import com.lanyan.system.domain.Courier;
import com.lanyan.system.service.ICourierService;
import com.lanyan.common.utils.poi.ExcelUtil;
import com.lanyan.common.core.page.TableDataInfo;

/**
 * 配送员Controller
 *
 * @author lanyan
 * @date 2024-11-26
 */
@RestController
@RequestMapping("/system/courier")
public class CourierController extends BaseController
{
    @Autowired
    private ICourierService courierService;
    @Autowired
    private TokenService tokenService;

    /**
     * 查询配送员列表
     */
    @PreAuthorize("@ss.hasPermi('system:courier:list')")
    @GetMapping("/list")
    @ApiImplicitParams({
          @ApiImplicitParam(name = "createBy", value = "创建者", dataType = "String", dataTypeClass = String.class),
          @ApiImplicitParam(name = "createTime", value = "创建时间", dataType = "Date", dataTypeClass = Date.class),
          @ApiImplicitParam(name = "updateBy", value = "更新者", dataType = "String", dataTypeClass = String.class),
          @ApiImplicitParam(name = "updateTime", value = "更新时间", dataType = "Date", dataTypeClass = Date.class),
          @ApiImplicitParam(name = "remark", value = "备注", dataType = "String", dataTypeClass = String.class),
          @ApiImplicitParam(name = "name", value = "昵称", dataType = "String", dataTypeClass = String.class),
          @ApiImplicitParam(name = "avatar", value = "头像", dataType = "String", dataTypeClass = String.class),
          @ApiImplicitParam(name = "phone", value = "电话", dataType = "String", dataTypeClass = String.class),
          @ApiImplicitParam(name = "estateId", value = "所属社区", dataType = "Long", dataTypeClass = Long.class),
          @ApiImplicitParam(name = "buildings", value = "负责楼栋", dataType = "String", dataTypeClass = String.class),
          @ApiImplicitParam(name = "loginName", value = "登录名", dataType = "String", dataTypeClass = String.class),
          @ApiImplicitParam(name = "loginPassword", value = "登录密码", dataType = "String", dataTypeClass = String.class),
          @ApiImplicitParam(name = "evaluateTotal", value = "评价总次数", dataType = "Long", dataTypeClass = Long.class),
          @ApiImplicitParam(name = "evaluateSum", value = "总评分", dataType = "Long", dataTypeClass = Long.class),
          @ApiImplicitParam(name = "pageNum", value = "第几页", dataType = "int", dataTypeClass = int.class),
          @ApiImplicitParam(name = "pageSize", value = "每页条数", dataType = "int", dataTypeClass = int.class),

    })
    public TableDataInfo list(Courier courier)
    {
        startPage();
        List<Courier> list = courierService.selectCourierList(courier);
        return getDataTable(list);
    }

    /**
     * 导出配送员列表
     */
    @PreAuthorize("@ss.hasPermi('system:courier:export')")
    @Log(title = "配送员", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Courier courier)
    {
        List<Courier> list = courierService.selectCourierList(courier);
        ExcelUtil<Courier> util = new ExcelUtil<Courier>(Courier.class);
        util.exportExcel(response, list, "配送员数据");
    }

    /**
     * 获取配送员详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:courier:query')")
    @GetMapping(value = "/{courierId}")
    public AjaxResult getInfo(@PathVariable("courierId") Long courierId)
    {
        return success(courierService.selectCourierByCourierId(courierId));
    }

    /**
     * 新增配送员
     */
    @PreAuthorize("@ss.hasPermi('system:courier:add')")
    @Log(title = "配送员", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiImplicitParams({
                    @ApiImplicitParam(name = "createBy", value = "创建者", dataType = "String", dataTypeClass = String.class),
                    @ApiImplicitParam(name = "createTime", value = "创建时间", dataType = "Date", dataTypeClass = Date.class),
                    @ApiImplicitParam(name = "updateBy", value = "更新者", dataType = "String", dataTypeClass = String.class),
                    @ApiImplicitParam(name = "updateTime", value = "更新时间", dataType = "Date", dataTypeClass = Date.class),
                    @ApiImplicitParam(name = "remark", value = "备注", dataType = "String", dataTypeClass = String.class),
                    @ApiImplicitParam(name = "name", value = "昵称", dataType = "String", dataTypeClass = String.class),
                    @ApiImplicitParam(name = "avatar", value = "头像", dataType = "String", dataTypeClass = String.class),
                    @ApiImplicitParam(name = "phone", value = "电话", dataType = "String", dataTypeClass = String.class),
                    @ApiImplicitParam(name = "estateId", value = "所属社区", dataType = "Long", dataTypeClass = Long.class),
                    @ApiImplicitParam(name = "buildings", value = "负责楼栋", dataType = "String", dataTypeClass = String.class),
                    @ApiImplicitParam(name = "loginName", value = "登录名", dataType = "String", dataTypeClass = String.class),
                    @ApiImplicitParam(name = "loginPassword", value = "登录密码", dataType = "String", dataTypeClass = String.class),
                    @ApiImplicitParam(name = "evaluateTotal", value = "评价总次数", dataType = "Long", dataTypeClass = Long.class),
                    @ApiImplicitParam(name = "evaluateSum", value = "总评分", dataType = "Long", dataTypeClass = Long.class),
    })
    public AjaxResult add(@RequestBody Courier courier)
    {
        return toAjax(courierService.insertCourier(courier));
    }

    /**
     * 修改配送员
     */
    @PreAuthorize("@ss.hasPermi('system:courier:edit')")
    @Log(title = "配送员", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiImplicitParams({
                    @ApiImplicitParam(name = "createBy", value = "创建者", dataType = "String", dataTypeClass = String.class),
                    @ApiImplicitParam(name = "createTime", value = "创建时间", dataType = "Date", dataTypeClass = Date.class),
                    @ApiImplicitParam(name = "updateBy", value = "更新者", dataType = "String", dataTypeClass = String.class),
                    @ApiImplicitParam(name = "updateTime", value = "更新时间", dataType = "Date", dataTypeClass = Date.class),
                    @ApiImplicitParam(name = "remark", value = "备注", dataType = "String", dataTypeClass = String.class),
                    @ApiImplicitParam(name = "name", value = "昵称", dataType = "String", dataTypeClass = String.class),
                    @ApiImplicitParam(name = "avatar", value = "头像", dataType = "String", dataTypeClass = String.class),
                    @ApiImplicitParam(name = "phone", value = "电话", dataType = "String", dataTypeClass = String.class),
                    @ApiImplicitParam(name = "estateId", value = "所属社区", dataType = "Long", dataTypeClass = Long.class),
                    @ApiImplicitParam(name = "buildings", value = "负责楼栋", dataType = "String", dataTypeClass = String.class),
                    @ApiImplicitParam(name = "loginName", value = "登录名", dataType = "String", dataTypeClass = String.class),
                    @ApiImplicitParam(name = "loginPassword", value = "登录密码", dataType = "String", dataTypeClass = String.class),
                    @ApiImplicitParam(name = "evaluateTotal", value = "评价总次数", dataType = "Long", dataTypeClass = Long.class),
                    @ApiImplicitParam(name = "evaluateSum", value = "总评分", dataType = "Long", dataTypeClass = Long.class),
    })
    public AjaxResult edit(@RequestBody Courier courier)
    {
        if (StringUtils.equals(courier.getIsFreeze(),"1")){
            Courier old = courierService.getById(courier.getCourierId());
            tokenService.delLoginUserByToken(old.getToken());
        }
        return toAjax(courierService.updateCourier(courier));
    }

    /**
     * 删除配送员
     */
    @PreAuthorize("@ss.hasPermi('system:courier:remove')")
    @Log(title = "配送员", businessType = BusinessType.DELETE)
	@DeleteMapping("/{courierIds}")
    public AjaxResult remove(@PathVariable Long[] courierIds)
    {
        return toAjax(courierService.deleteCourierByCourierIds(courierIds));
    }
}
